package com.be.beadmin.basicData.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.be.beadmin.basicData.dto.input.MaterialInfoDto;
import com.be.beadmin.basicData.dto.output.MaterialInfoEx;
import com.be.beadmin.basicData.dto.output.MaterialInfoExcelEx;
import com.be.beadmin.basicData.entity.MaterialInfo;
import com.be.beadmin.basicData.service.MaterialInfoService;
import com.be.beadmin.common.entity.PageEntity;
import com.be.beadmin.common.utils.ResultModel;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author cms
* @date 2023-12-07
**/
@Api(tags = {"基础数据-物料主数据"})
@RestController
public class MaterialInfoController {

    @Autowired
    private  MaterialInfoService materialInfoService;

    @ApiOperation("根据属性获取物料主数据列表")
    @PostMapping(value = "/basicData/getMaterialInfoByMaterialProperty")
    public ResultModel getMaterialInfoByMaterialProperty(@RequestParam String materialProperty)  {
        List<MaterialInfo> materialInfoExList = materialInfoService.getMaterialInfoByMaterialProperty(materialProperty);
        return ResultModel.success(materialInfoExList) ;
    }

    @PostMapping(value = "/basicData/getMaterialInfoByPage")
    public ResultModel getMaterialInfoByPage(@RequestBody PageEntity pageEntity)  {
        return ResultModel.success(materialInfoService.getMaterialInfoByPage(pageEntity)) ;
    }

    @PostMapping("/basicData/addMaterialInfo")
    public ResultModel addMaterialInfo(@RequestBody MaterialInfoDto materialInfoDto){
        materialInfoService.addMaterialInfo(materialInfoDto);
        return ResultModel.success(ResultModel.ADD);
    }

    @PostMapping("/basicData/delMaterialInfo")
    public ResultModel delMaterialInfo(@RequestBody MaterialInfoDto materialInfoDto){
        materialInfoService.delMaterialInfo(materialInfoDto.getId());
        return ResultModel.success(ResultModel.DELETE);
    }


    @PostMapping("/basicData/updateMaterialInfo")
    public ResultModel updateMaterialInfo(@RequestBody MaterialInfoDto materialInfoDto){
        materialInfoService.updateMaterialInfo(materialInfoDto);
        return ResultModel.success(ResultModel.UPDATE);
    }


    @PostMapping("/basicData/exportMaterialInfo")
    public void exportMaterialInfo(HttpServletResponse response ,@RequestBody PageEntity pageEntity) throws IOException {

        List<MaterialInfoEx> materialInfoExes = new ArrayList<>();

        if(pageEntity.getPageSize() != 0){
            // 导出模板
            Page<MaterialInfoEx> itemPage = materialInfoService.getMaterialInfoByPage(pageEntity);
            materialInfoExes = itemPage.getContent();
        }
        List<MaterialInfoExcelEx> materialInfoExcelEx = materialInfoExes.stream().map(source -> {
            MaterialInfoExcelEx target = new MaterialInfoExcelEx();
            BeanUtils.copyProperties(source, target);
            return target;
        }).collect(Collectors.toList());
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("MaterialInfo接口", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xls");
        EasyExcel.write(response.getOutputStream(), MaterialInfoExcelEx.class).excelType(ExcelTypeEnum.XLS).sheet("sheet1").doWrite(materialInfoExcelEx);
    }

    @ApiOperation("Excel导入物料主数据")
    @PostMapping("/basicData/uploadMaterialInfo")
    public ResultModel uploadWorkhourAndCycle(@RequestParam("file") MultipartFile file) throws IOException {
        materialInfoService.uploadMaterialInfo(file);
        return ResultModel.success("上传成功");
    }

    @ApiOperation("根据物料编码获取物料名称")
    @PostMapping("/basicData/getMaterialNameByMaterialCode")
    public ResultModel getMaterialNameByMaterialCode(@RequestParam String  materialCode){
        return ResultModel.success(materialInfoService.getMaterialNameByMaterialCode(materialCode));
    }
}